package com.superteam.zzl.Des;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.superteam.zzl.activity.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/1/7.
 */
public class NewFragment extends Fragment implements GridView.OnItemClickListener{
    // private ActivityMainBinding amb;
    private List<String> list;
    private List<City.DataBean> datalist;
    private  List<String> tuijianlist;
    private CityAdapter adapter;
    private GridView gv;

    private TextView t;
    public static NewFragment getInstance(int type)
    {
        Bundle bundle=new Bundle();
        bundle.putInt("type",type);
        NewFragment fragment=new NewFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment,null);
        list=new ArrayList<>();

        datalist=new ArrayList<>();
        adapter=new CityAdapter(getContext(),datalist);
       list.add(ContentURLs.NorthAmerica);
       list.add(ContentURLs.Pacific);
       list.add(ContentURLs.Africa);
       list.add(ContentURLs.SouthAmerica);
       list.add(ContentURLs.Europe);
       list.add(ContentURLs.Destination);
       list.add(ContentURLs.Asia);

       // t=(TextView)view.findViewById(R.id.tv);
        gv=(GridView) view.findViewById(R.id.frgv);
       // amb= DataBindingUtil.setContentView(this,R.layout.fragment);
        loadData();
        init();
        return view;
    }

    private void init() {

        gv.setAdapter(adapter);
        gv.setOnItemClickListener(this);




    }

    private void loadData() {
        int type=getArguments().getInt("type");
//        Toast.makeText(getContext(),type,Toast.LENGTH_SHORT).show();
        new JsonAsyncTask(getContext(), new JsonAsyncTask.CallBack() {
            @Override
            public void sendData(String json) {
                City city=new Gson().fromJson(json,City.class);
                datalist.addAll(city.data);
                adapter.notifyDataSetChanged();

            }
        }).execute(list.get(type));
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int type=getArguments().getInt("type");
      //  Toast.makeText(getContext(),position+"sss",Toast.LENGTH_SHORT).show();

        tuijianlist=new ArrayList<>();
        switch (type){

            case 0:
                tuijianlist.add(ContentURLs.America);
                tuijianlist.add(ContentURLs.Canada);

                break;
            case 1:
                tuijianlist.add(ContentURLs.Australian);
                tuijianlist.add(ContentURLs.NewZealand);
                tuijianlist.add(ContentURLs.Palau_1);
                break;
            case 2:
                tuijianlist.add(ContentURLs.SouthAfrica);
                tuijianlist.add(ContentURLs.Egypt);
                tuijianlist.add(ContentURLs.Morocco);
                tuijianlist.add(ContentURLs.Mauritius);
                break;
            case 3:
                tuijianlist.add(ContentURLs.Brazil);
                tuijianlist.add(ContentURLs.Argentina);
                break;
            case 4:
                tuijianlist.add(ContentURLs.Turkey);
                tuijianlist.add(ContentURLs.Italy);
                tuijianlist.add(ContentURLs.Greece);
                tuijianlist.add(ContentURLs.France);
                tuijianlist.add(ContentURLs.England);
                tuijianlist.add(ContentURLs.Germany);
                tuijianlist.add(ContentURLs.Spain);
                tuijianlist.add(ContentURLs.Russia);
                tuijianlist.add(ContentURLs.Ireland);
                tuijianlist.add(ContentURLs.Austria);
                tuijianlist.add(ContentURLs.Portugal);
                tuijianlist.add(ContentURLs.Switzerland);
                tuijianlist.add(ContentURLs.Netherlands);
                tuijianlist.add(ContentURLs.Belgium);
                tuijianlist.add(ContentURLs.Czech);
                tuijianlist.add(ContentURLs.Poland);
                tuijianlist.add(ContentURLs.Norway);
                tuijianlist.add(ContentURLs.Finland);
                tuijianlist.add(ContentURLs.Sweden);
                tuijianlist.add(ContentURLs.Denmark);

                break;
            case 5:
                tuijianlist=new ArrayList<>();
                tuijianlist.add(ContentURLs.Seoul_1);
                //  tuijianlist.add(ContentURLs.Seoul_2);
                tuijianlist.add(ContentURLs.Paris_1);
                // tuijianlist.add(ContentURLs.Paris_2);
                tuijianlist.add(ContentURLs.Jeju_1);
                // tuijianlist.add(ContentURLs.Jeju_2);
                tuijianlist.add(ContentURLs.Sabah_1);
                // tuijianlist.add(ContentURLs.Sabah_2);
                tuijianlist.add(ContentURLs.KohSamui_1);
                // tuijianlist.add(ContentURLs.KohSamui_2);
                tuijianlist.add(ContentURLs.Phuket_1);
                // tuijianlist.add(ContentURLs.Phuket_2);
                tuijianlist.add(ContentURLs.Bangkok_1);
                // tuijianlist.add(ContentURLs.Bangkok_2);
                tuijianlist.add(ContentURLs.Chiengmai_1);
                // tuijianlist.add(ContentURLs.Chiengmai_2);
                tuijianlist.add(ContentURLs.HuaHin_1);
                // tuijianlist.add(ContentURLs.HuaHin_2);
                tuijianlist.add(ContentURLs.Taiwan_1);
                // tuijianlist.add(ContentURLs.Taiwan_2);

//                intent.putExtra("shuju1", tuijianlist.get(2*position));
//                intent.putExtra("shuju2", tuijianlist.get((2*position))+1);

                break;
            case 6:

                tuijianlist.add(ContentURLs.Korea);
                tuijianlist.add(ContentURLs.Thailand);
                tuijianlist.add(ContentURLs.Japanese);
                tuijianlist.add(ContentURLs.Singapore_1);
                tuijianlist.add(ContentURLs.Malaysia);
                tuijianlist.add(ContentURLs.Cambodia_1);
                tuijianlist.add(ContentURLs.SriLanka_1);
                tuijianlist.add(ContentURLs.UnitedArabEmirates);
                tuijianlist.add(ContentURLs.Vietnamese);
                tuijianlist.add(ContentURLs.Philippines);
                tuijianlist.add(ContentURLs.Saipan_1);
                tuijianlist.add(ContentURLs.Maldives);
                tuijianlist.add(ContentURLs.India);
                tuijianlist.add(ContentURLs.Indonesia);

                break;
        }

        String flag=tuijianlist.get(position);
        String str[]=flag.split("/");

        // Toast.makeText(getContext(),str[5],Toast.LENGTH_SHORT).show();

        if (str[5].equals("board")){
            Intent intent=new Intent(getContext(),Second.class);
            intent.putExtra("shuju1",tuijianlist.get(position));
            startActivity(intent);
        }else if (str[5].equals("place")){
            Intent intent=new Intent(getActivity(),CountryActivity.class);
            intent.putExtra("shuju1",tuijianlist.get(position));
            startActivity(intent);
        }
    }
}

























